package l2hc.gameserver.handler.voicecommands.impl;

import l2hc.commons.net.utils.NetList;
import l2hc.gameserver.Config;
import l2hc.gameserver.data.htm.HtmCache;
import l2hc.gameserver.handler.voicecommands.IVoicedCommandHandler;
import l2hc.gameserver.model.Player;
import l2hc.gameserver.network.loginservercon.LoginServerCommunication;
import l2hc.gameserver.network.loginservercon.gspackets.LockAccountIP;
import l2hc.gameserver.scripts.Functions;

public class Lock extends Functions implements IVoicedCommandHandler
{
private final String[] _commandList = { "lock_ip" };

private static String defaultPage = "command/lock.htm";

private void showDefaultPage(Player activeChar)
{
  String html = HtmCache.getInstance().getNotNull(defaultPage, activeChar);
  html = html.replaceFirst("%IP%", activeChar.getIP());
  show(html, activeChar);
}

public boolean useVoicedCommand(String command, Player activeChar, String target)
{
  if (!Config.SERVICES_LOCK_ACCOUNT_IP) {
    return false;
  }
  if ((command.equals("lock")) && ((target == null) || (target.equals(""))))
  {
    showDefaultPage(activeChar);
    return true;
  }

  String[] param = target.split(" ");

  if (param.length > 0)
  {
    int time = 604800;
    String ip = activeChar.getIP();
    if (param.length > 1)
      for (int i = 1; i < param.length; i++)
        if (param[i].contains("."))
          ip = param[i];
        else
          try
          {
            time = Integer.parseInt(param[1]) * 60 * 60 * 24;
          }
          catch (NumberFormatException e)
          {
          }
    boolean invalid = false;
    try
    {
      NetList allowedList = new NetList();
      if (!allowedList.isInRange(activeChar.getIP()))
      {
        activeChar.sendMessage("wtf");
        invalid = true;
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
      invalid = true;
    }
    if (invalid)
    {
      activeChar.sendMessage("Invalid IP mask: you 'll be unable to login from your current address!");
      return false;
    }

    time = Math.min(time, 1209600);
    if (param[0].equalsIgnoreCase("on"))
    {
      LoginServerCommunication.getInstance().sendPacket(new LockAccountIP(activeChar.getAccountName(), ip, time));
      if (activeChar.isLangRus())
        activeChar.sendMessage("Акаунт закрыт для доступа по указанному ИП адресу!");
      else {
        activeChar.sendMessage("This account is locked to login only for entered IP address");
      }
      return true;
    }

    if (param[0].equalsIgnoreCase("off"))
    {
      LoginServerCommunication.getInstance().sendPacket(new LockAccountIP(activeChar.getAccountName(), "*", -1));
      if (activeChar.isLangRus())
        activeChar.sendMessage("Акаунт открыт для доступа с любого ИП адреса!");
      else
        activeChar.sendMessage("This account is unlocked to login on any IP address");
      return true;
    }
  }

  showDefaultPage(activeChar);
  return true;
}

public String[] getVoicedCommandList()
{
  return this._commandList;
}
}